package data

import (
	"fmt"
	"yunj/pkg/global"
	"yunj/pkg/yunj/app/model"
	"yunj/pkg/yunj/core/db"
)

// 初始化岗位数据库
func InitPostionDbData() (err error) {
	postionDb := db.Model(&model.AdminPostion{})
	tableName := postionDb.GetTableName()
	// 创建数据表
	tableSql := "CREATE TABLE IF NOT EXISTS `" + tableName + "` (" +
		"`id` int NOT NULL AUTO_INCREMENT," +
		"`name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '名称'," +
		"`code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '标识'," +
		"`intro` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '简介'," +
		"`sort` int unsigned NOT NULL DEFAULT '0' COMMENT '排序'," +
		"`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP," +
		"`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
		"`state` tinyint unsigned NOT NULL DEFAULT '11' COMMENT '11 正常 | 22 回收站 | 33 已删除'," +
		"PRIMARY KEY (`id`) USING BTREE," +
		"UNIQUE KEY `uk_code` (`code`) USING BTREE" +
		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='岗位表';"
	err = global.MySQL.Exec(tableSql).Error
	if err != nil {
		err = fmt.Errorf("创建数据表%s失败！%v", tableName, err)
		return
	}
	return
}

// 初始化岗位角色数据库
func InitPostionRoleDbData() (err error) {
	postionRoleDb := db.Model(&model.AdminPostionRole{})
	tableName := postionRoleDb.GetTableName()
	// 创建数据表
	tableSql := "CREATE TABLE IF NOT EXISTS `" + tableName + "` (" +
		"`postion_id` int NOT NULL," +
		"`role_id` int NOT NULL," +
		"PRIMARY KEY (`postion_id`,`role_id`) USING BTREE" +
		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='岗位角色关系表';"
	err = global.MySQL.Exec(tableSql).Error
	if err != nil {
		err = fmt.Errorf("创建数据表%s失败！%v", tableName, err)
	}
	return
}
